package com.example.mall.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.mall.domain.RolePermission;
import com.example.mall.dto.RolePermissionDto;
import com.example.mall.mapper.RolePermissionMapper;
import com.example.mall.query.PageDTO;
import com.example.mall.query.RolePermissionQuery;
import com.example.mall.service.IRolePermissionService;
import com.example.mall.util.AssertUtil;
import com.example.mall.util.CodeGenerateUtils;
import com.example.mall.util.CopyUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * 角色权限关联表 服务实现类
 * </p>
 *
 * @author whale
 * @since 2025-04-30
 */
@Service
public class RolePermissionServiceImpl extends ServiceImpl<RolePermissionMapper, RolePermission> implements IRolePermissionService {

    @Autowired
    public RolePermissionMapper rolePermissionMapper;

    @Override
    public PageDTO<RolePermissionDto> queryPage(RolePermissionQuery rolePermissionQuery) {
        if (rolePermissionQuery == null) {
            rolePermissionQuery = new RolePermissionQuery();
        }
        Page<RolePermission> page = rolePermissionQuery.toMpPageByDefaultCreateTimeAsc();
        Page<RolePermission> cartPage = lambdaQuery()
                .like(rolePermissionQuery.getRolePermissionId()!= null, RolePermission::getRolePermissionId, rolePermissionQuery.getRolePermissionId())
                .like(rolePermissionQuery.getRoleId()!= null, RolePermission::getRoleId, rolePermissionQuery.getRoleId())
                .like(rolePermissionQuery.getPermissionId()!= null, RolePermission::getPermissionId, rolePermissionQuery.getPermissionId())
                .page(page);
        return PageDTO.of(cartPage, RolePermissionDto.class);
    }

    @Override
    public RolePermission queryRolePermissionById(Long rolePermissionId) {
        return rolePermissionMapper.selectById(rolePermissionId);
    }

    @Override
    public List<RolePermission> queryRolePermissionList() {
        return this.list();
    }

    @Override
    public void insert(RolePermissionDto rolePermissionDto) {
        RolePermission rolePermission = new RolePermission();
        CopyUtils.copyProperties(rolePermissionDto, rolePermission);
        rolePermission.setPermissionId(CodeGenerateUtils.generateUniqueId());
        rolePermissionMapper.insert(rolePermission);
    }

    @Override
    public void delete(Long rolePermissionId) {
        AssertUtil.isNotNull(rolePermissionId, "角色权限表id不能为空");
        RolePermission rolePermission = rolePermissionMapper.selectById(rolePermissionId);
        AssertUtil.isNotNull(rolePermission, "角色权限表不存在");
        rolePermissionMapper.deleteById(rolePermissionId);
    }

    @Override
    public void update(RolePermissionDto rolePermissionDto) {
        AssertUtil.isNotNull(rolePermissionDto, "角色权限表不能为空");
        RolePermission rolePermission = rolePermissionMapper.selectById(rolePermissionDto.getRolePermissionId());
        AssertUtil.isNotNull(rolePermission, "角色权限表不存在");
        CopyUtils.copyProperties(rolePermissionDto, rolePermission);
        rolePermissionMapper.updateById(rolePermission);
    }

    @Override
    public void deleteBatch(List<Long> rolePermissionIds) {
        AssertUtil.isNotNull(rolePermissionIds, "角色权限表id不能为空");
        List<RolePermission> rolePermissions = rolePermissionMapper.selectBatchIds(rolePermissionIds);
        AssertUtil.isNotNull(rolePermissions, "角色权限表不存在");
        rolePermissionMapper.deleteBatchIds(rolePermissionIds);
    }
}
